import { notification } from 'antd';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';

// 设置 dayjs 中文支持
dayjs.locale('zh-cn');

/**
 * 格式化日期
 * @param date 日期
 * @param format 格式化字符串
 * @returns 格式化后的日期字符串
 */
export const formatDate = (
  date: Date | string | number,
  format = 'YYYY-MM-DD HH:mm:ss'
): string => {
  return dayjs(date).format(format);
};

/**
 * 显示成功通知
 * @param message 消息内容
 * @param description 描述信息
 */
export const showSuccessNotification = (message: string, description?: string) => {
  notification.success({
    message,
    description,
    placement: 'topRight',
    duration: 3,
  });
};

/**
 * 显示错误通知
 * @param message 消息内容
 * @param description 描述信息
 */
export const showErrorNotification = (message: string, description?: string) => {
  notification.error({
    message,
    description,
    placement: 'topRight',
    duration: 5,
  });
};

/**
 * 显示警告通知
 * @param message 消息内容
 * @param description 描述信息
 */
export const showWarningNotification = (message: string, description?: string) => {
  notification.warning({
    message,
    description,
    placement: 'topRight',
    duration: 4,
  });
};

/**
 * 复制文本到剪贴板
 * @param text 要复制的文本
 * @returns Promise<boolean>
 */
export const copyToClipboard = async (text: string): Promise<boolean> => {
  try {
    await navigator.clipboard.writeText(text);
    showSuccessNotification('复制成功', '内容已复制到剪贴板');
    return true;
  } catch (error) {
    showErrorNotification('复制失败', '无法复制到剪贴板');
    return false;
  }
};

/**
 * 延迟执行函数
 * @param ms 延迟时间（毫秒）
 * @returns Promise<void>
 */
export const delay = (ms: number): Promise<void> => {
  return new Promise(resolve => setTimeout(resolve, ms));
};

/**
 * 生成随机ID
 * @param length ID长度
 * @returns 随机ID字符串
 */
export const generateId = (length = 8): string => {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  for (let i = 0; i < length; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return result;
};
